package cn.wolfcode.servlet;

import cn.wolfcode.domain.User;
import cn.wolfcode.mapper.UserMapper;
import cn.wolfcode.utils.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/denglu")
public class dengluServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        String username = req.getParameter("username");
        String pwd = req.getParameter("password");
        String yan = req.getParameter("yan");
        String yanzhengma = req.getParameter("yanzheng");
        System.out.println(yanzhengma);
        SqlSession session = MybatisUtil.openSession();
        UserMapper userMapper=session.getMapper(UserMapper.class);
        if(yan.equals(yanzhengma)){
            User u1=userMapper.selectu(username);
            if(u1!=null){
                User u2=userMapper.selectup(username,pwd);
                if(u2!=null){
                    req.getSession().setAttribute("username",username);
                    req.getSession().setAttribute("uid",u2.getId());
                    req.getRequestDispatcher("/chaxunServlet").forward(req,resp);
                }else {
                    req.setAttribute("errorMsg", "密码错误");
                    req.getRequestDispatcher("/index.jsp").forward(req, resp);
                    System.out.println("密码错误");
                }
            }else {
                req.setAttribute("errorMsg", "没有该账号");
                req.getRequestDispatcher("/index.jsp").forward(req, resp);
                System.out.println("没有该账号");

            }
        }else {
            req.setAttribute("errorMsg", "验证码错误");
            req.getRequestDispatcher("/index.jsp").forward(req, resp);
            System.out.println("验证码错误");

        }

    }
}
